<?php

	include('../Vendor/adodb5/adodb.inc.php');
	
	$dbName 		= 'dbinmo';
	$dbUserName 	= 'root';
	$dbUserPass 	= '';


	$db = NewADOConnection('mysql');
	$db->Connect('localhost', $dbUserName, $dbUserPass, $dbName);
    $db->execute("SET NAMES utf8");		

	$countryId = $_POST['countryId'];
	
	$getZonesQuery = 'SELECT z.descripcion,z.id,r.descripcion as region_nombre,r.id as region_id 
												FROM zonas z
												LEFT JOIN regiones r
												ON z.region_id = r.id
												WHERE r.pais_id = '.$countryId;
												
	$zonas = $db->getAll($getZonesQuery);
		
	$regiones = array();
	foreach ($zonas as $zona){	
		$regiones[$zona['region_id']]['zonas'][] = array(
									'id' => $zona['id'],
									'descripcion' => $zona['descripcion']
								);
		$regiones[$zona['region_id']]['descripcion'] = $zona['region_nombre'];
	}
	
	
	ob_start();

?>

<option value="">Zona</option>
<?php foreach($regiones as $region_id => $region) { ?>
	<option value="<?php echo $region_id; ?>"><?php echo strToUpper($region['descripcion']); ?></option>
	<?php foreach($region['zonas'] as $zona) { ?>
		<option value="<?php echo $zona['id']; ?>">&nbsp;&nbsp;&nbsp;<?php echo $zona['descripcion']; ?></option>
	<?php } ?>
<?php } ?>

<?php 
	
$output = ob_get_clean();

echo $output;

?>
